package com.sasis.dao.ibatis;

import java.util.Collection;
import java.util.List;

import com.sasis.dao.RoleDao;
import com.sasis.model.Role;
import com.sasis.util.PageList;
import com.sasis.util.PageRequest;

/**
 * This class interacts with iBatis's SQL Maps to save/delete and
 * retrieve Role objects.
 *
 * <p>
 * <a href="RoleDaoiBatis.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class RoleDaoiBatis extends BaseDaoiBATIS implements RoleDao {
	
	public SqlMapClientWrapper getSqlMapClientWrapper() {
		return new SqlMapClientWrapper(Role.class, super.getSqlMapClientTemplate());
	}
	
	public PageList findAll(PageRequest pageRequest) {
		return getSqlMapClientWrapper().queryForPageList("findAll", "countAll", null, pageRequest);
	}
	
	public PageList findByLikeName(String name, PageRequest pageRequest) {
		return getSqlMapClientWrapper().queryForPageList("findByLikeName", "countByLikeName", name, pageRequest);
	}
	
	public Collection findByUserId(Long userId) {
		return getSqlMapClientWrapper().queryForList("findByUserId", userId);
	}

    public List getRoles() {
        return getSqlMapClientTemplate().queryForList("getRoles", null);
    }
    
    public Role loadRole(Long id) {
    	return (Role) getSqlMapClientTemplate().queryForObject("loadRole", id);
    }
    
    public Role getRoleByName(String name) {
        return (Role) getSqlMapClientTemplate().queryForObject("getRoleByName", name);
    }

    public void saveRole(final Role role) {
        if (role.getId() == null) {
            getSqlMapClientTemplate().update("addRole", role);
        } else {
            getSqlMapClientTemplate().update("updateRole", role);
        }
    }

    public void removeRole(String rolename) {
        getSqlMapClientTemplate().update("deleteRole", rolename);
    }

}
